const fs = require('fs');
const path = require('path');

function news() {
	const fullPath = path.join(__dirname, './news.json');
	const news = fs.readFileSync(fullPath, 'utf-8');
	return JSON.parse(news);
}

function studentsInfo() {
	const fullPath = path.join(__dirname, './info.json');
	const students = fs.readFileSync(fullPath, 'utf-8');
	return JSON.parse(students);
}

function luckyStudent() {
	// 绝对路径
	const fullPath = path.join(__dirname, './info.json');
	// 调用已经抽取好的方法 获取学员数据
	const studentArr = studentsInfo();
	// console.log('studentArr:', studentArr)
	// 抽取随机
	const index = parseInt(Math.random() * studentArr.length);
	// count++
	const luckystar = studentArr[index];
	luckystar.count++;
	// 保存
	fs.writeFileSync(fullPath, JSON.stringify(studentArr));
	// 返回
	return luckystar;
}

module.exports = {
	news,
	studentsInfo,
	luckyStudent,
};
